package pl.pelcra.cesar.spider.webcrawler.sites;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import pl.pelcra.cesar.data.interfaces.ICommonData;
import pl.pelcra.cesar.spider.data.MapLangUrl;
import pl.pelcra.cesar.spider.webcrawler.CSWebURL;
import pl.pelcra.cesar.spider.webcrawler.interfaces.BasePageWebTasks;
import pl.pelcra.cesar.spider.webcrawler.interfaces.FilterRules;
import pl.pelcra.cesar.spider.webcrawler.interfaces.HttpRequestMethod;
import pl.pelcra.cesar.spider.webcrawler.interfaces.IPageActions;
import pl.pelcra.cesar.spider.webcrawler.interfaces.WebTask;
import pl.pelcra.cesar.tools.Tools;
import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.parser.HtmlParseData;
import edu.uci.ics.crawler4j.url.WebURL;

public class OswWawPageWebTasks extends BasePageWebTasks {

	public OswWawPageWebTasks() {

	}

	public OswWawPageWebTasks(String parameters) {
		super(parameters);
	}

	@Override
	public void init(WebURL startWebUrl, ICommonData<MapLangUrl> threadData) {
		this.setSpiderThreadData(threadData);

		WebTask enWikiWebTask = new WebTask(null);
		enWikiWebTask
				.addParametrToRules("/en/publikacje/.+/\\d{4,4}-\\d{2,2}-\\d{2,2}/.+");
		enWikiWebTask.addIPageActions(new IPageActions() {

			@Override
			public void afterProcessPage(WebTask wikiWebTask, Page page,
					HtmlParseData htmlParseData) {

				MapLangUrl map = new MapLangUrl();
				// http://www.osw.waw.pl/pl/publikacje/tydzien-na-wschodzie/2009-11-18/oredzie-miedwiediewa-wezwanie-do-modernizacji
				// http://www.osw.waw.pl/pl/print/publikacje/tydzien-na-wschodzie/2009-11-18/oredzie-miedwiediewa-wezwanie-do-modernizacji

				// http://www.osw.waw.pl/en/publikacje/eastweek/2009-11-18/medvedevs-address-a-call-modernisation
				// http://www.osw.waw.pl/en/print/publikacje/eastweek/2009-11-18/medvedevs-address-a-call-modernisation
				WebURL webUrl = wikiWebTask.getCurWebURL();
				webUrl.setURL(webUrl.getURL().replace(
						"http://www.osw.waw.pl/pl/",
						"http://www.osw.waw.pl/pl/print/"));
				map.put("pl", new CSWebURL(webUrl));
				if (wikiWebTask.getListWebUrl().size() > 0) {
					WebURL enWebUrl = wikiWebTask.getListWebUrl().get(0);
					enWebUrl.setURL(enWebUrl.getURL().replace(
							"http://www.osw.waw.pl/en/",
							"http://www.osw.waw.pl/en/print/"));
					map.put("en", new CSWebURL(enWebUrl));
					OswWawPageWebTasks.this.getSpiderThreadData().put(map);
				}
			}

			@Override
			public void beforeProcessPage(HtmlParseData htmlParseData) {
			}

			@Override
			public boolean filterUrl(WebURL webURL, WebTask webTask) {
				FilterRules filterFules = webTask.getFilterRules();
				String regexUrl = null;
				if (filterFules.getListParameter().size() == 2) {
					if (filterFules.getListParameter(0) instanceof String
							&& filterFules.getListParameter(1) instanceof WebURL) {

						regexUrl = (String) filterFules.getListParameter(0);
					}
				}

				if (webURL.getPath().matches(regexUrl)) {
					return true;
				}
				return false;
			}

			@Override
			public void finishProcessWebTasks(WebTask wikiWebTask) {
			}

			@Override
			public WebURL getFilterParallelWebUrl(FilterRules wikiFilterRules,
					List<WebURL> listWebUrl) {
				return null;
			}

			@Override
			public HttpRequestMethod getHttpRequestMethod() {
				return HttpRequestMethod.GET;
			}

			@Override
			public List<BasicNameValuePair> getParameterValuePairs(
					HashMap<String, String> mapParameters) {
				return null;
			}

			@Override
			public boolean isCookieRequired() {
				return false;
			}

			@Override
			public boolean isNextParallelPage(WebTask wikiWebTask,
					WebURL currentParallelWebUrl, WebURL filterParallelWebUrl) {
				return false;
			}

			@Override
			public boolean isParallelProcess() {
				return false;
			}

			@Override
			public void processListURLs(WebTask wikiWebTask) {
			}
		});

		WebTask articlesWikiWebTask = new WebTask(enWikiWebTask);
		articlesWikiWebTask
				.addParametrToRules("/\\d{4,4}-\\d{2,2}-\\d{2,2}/.+");
		articlesWikiWebTask
				.addParametrToRules("/\\d{4,4}-\\d{2,2}-\\d{2,2}/spis-tresci");
		articlesWikiWebTask
				.addParametrToRules("/\\d{4,4}-\\d{2,2}-\\d{2,2}/numer-.+");
		articlesWikiWebTask
				.addParametrToRules("/\\d{4,4}-\\d{2,2}-\\d{2,2}/\\d{1,2}-\\d{1,3}");
		articlesWikiWebTask
				.addParametrToRules("/\\d{4,4}-\\d{2,2}-\\d{2,2}/raport");
		articlesWikiWebTask
				.addParametrToRules("/\\d{4,4}-\\d{2,2}-\\d{2,2}/zeszyt-nr-\\d{1,3}");

		articlesWikiWebTask.addParametrToRules("?page=");
		articlesWikiWebTask.addIPageActions(new IPageActions() {

			@Override
			public void afterProcessPage(WebTask wikiWebTask, Page page,
					HtmlParseData htmlParseData) {
			}

			@Override
			public void beforeProcessPage(HtmlParseData htmlParseData) {
				// TODO Auto-generated method stub

			}

			@Override
			public boolean filterUrl(WebURL webURL, WebTask webTask) {

				FilterRules filterRules = webTask.getFilterRules();
				String matchRegexUrl = null;
				String notMatchRegexUrl1 = null;
				String notMatchRegexUrl2 = null;
				String notMatchRegexUrl3 = null;
				String notMatchRegexUrl4 = null;
				String notMatchRegexUrl5 = null;
				if (filterRules.getListParameter().size() == 8) {
					if (filterRules.getListParameter(0) instanceof String
							&& filterRules.getListParameter(1) instanceof String
							&& filterRules.getListParameter(2) instanceof String
							&& filterRules.getListParameter(3) instanceof String
							&& filterRules.getListParameter(4) instanceof String
							&& filterRules.getListParameter(5) instanceof String
							&& filterRules.getListParameter(6) instanceof String
							&& filterRules.getListParameter(7) instanceof WebURL) {
						String url = ((WebURL) filterRules.getListParameter(7))
								.getURL().toLowerCase();
						url = url.substring(0, url.indexOf((String) filterRules
								.getListParameter(6)));

						matchRegexUrl = url
								+ (String) filterRules.getListParameter(0);
						notMatchRegexUrl1 = url
								+ (String) filterRules.getListParameter(1);

						notMatchRegexUrl2 = url
								+ (String) filterRules.getListParameter(2);

						notMatchRegexUrl3 = url
								+ (String) filterRules.getListParameter(3);

						notMatchRegexUrl4 = url
								+ (String) filterRules.getListParameter(4);

						notMatchRegexUrl5 = url
								+ (String) filterRules.getListParameter(5);
					}
				}

				if (matchRegexUrl != null
						&& webURL.getURL().toLowerCase().matches(matchRegexUrl)) {
					if ((notMatchRegexUrl1 != null && !webURL.getURL()
							.toLowerCase().matches(notMatchRegexUrl1))
							|| notMatchRegexUrl1 == null) {

						if ((notMatchRegexUrl2 != null && !webURL.getURL()
								.toLowerCase().matches(notMatchRegexUrl2))
								|| (notMatchRegexUrl2 == null)) {

							if ((notMatchRegexUrl3 != null && !webURL.getURL()
									.toLowerCase().matches(notMatchRegexUrl3))
									|| (notMatchRegexUrl3 == null)) {

								if ((notMatchRegexUrl4 != null && !webURL
										.getURL().toLowerCase()
										.matches(notMatchRegexUrl4))
										|| (notMatchRegexUrl4 == null)) {

									if ((notMatchRegexUrl5 != null && !webURL
											.getURL().toLowerCase()
											.matches(notMatchRegexUrl5))
											|| (notMatchRegexUrl5 == null)) {
										return true;
									}
								}
							}
						}
					}
				}
				return false;
			}

			@Override
			public void finishProcessWebTasks(WebTask wikiWebTask) {
			}

			@Override
			public WebURL getFilterParallelWebUrl(FilterRules wikiFilterRules,
					List<WebURL> listWebUrl) {
				return null;
			}

			@Override
			public HttpRequestMethod getHttpRequestMethod() {
				return HttpRequestMethod.GET;
			}

			@Override
			public List<BasicNameValuePair> getParameterValuePairs(
					HashMap<String, String> mapParameters) {
				return null;
			}

			@Override
			public boolean isCookieRequired() {
				return false;
			}

			@Override
			public boolean isNextParallelPage(WebTask wikiWebTask,
					WebURL currentParallelWebUrl, WebURL filterParallelWebUrl) {
				return false;
			}

			@Override
			public boolean isParallelProcess() {
				return false;
			}

			@Override
			public void processListURLs(WebTask wikiWebTask) {
			}
		});

		WebTask pagesWikiWebTask = new WebTask(articlesWikiWebTask);
		pagesWikiWebTask.addParametrToRules("?page=");
		pagesWikiWebTask.addIPageActions(new IPageActions() {

			@Override
			public void afterProcessPage(WebTask wikiWebTask, Page page,
					HtmlParseData htmlParseData) {
			}

			@Override
			public void beforeProcessPage(HtmlParseData htmlParseData) {
			}

			@Override
			public boolean filterUrl(WebURL webURL, WebTask webTask) {
				FilterRules filterRules = webTask.getFilterRules();

				for (WebURL tmpWebURL : webTask.getListWebUrl()) {
					if (tmpWebURL.getURL().equalsIgnoreCase(webURL.getURL())) {
						return false;
					}
				}

				String startUrl = null;
				if (filterRules.getListParameter().size() == 2) {
					if (filterRules.getListParameter(0) instanceof String
							&& filterRules.getListParameter(1) instanceof WebURL) {
						startUrl = ((WebURL) filterRules.getListParameter(1))
								.getPath().toLowerCase()
								+ (String) filterRules.getListParameter(0);
					}
				}

				if (startUrl != null
						&& webURL.getURL().toLowerCase()
								.contains(startUrl.toLowerCase())) {
					return true;
				}
				return false;
			}

			@Override
			public void finishProcessWebTasks(WebTask wikiWebTask) {
			}

			@Override
			public WebURL getFilterParallelWebUrl(FilterRules wikiFilterRules,
					List<WebURL> listWebUrl) {
				return null;
			}

			@Override
			public HttpRequestMethod getHttpRequestMethod() {
				return HttpRequestMethod.GET;
			}

			@Override
			public List<BasicNameValuePair> getParameterValuePairs(
					HashMap<String, String> mapParameters) {
				return null;
			}

			@Override
			public boolean isCookieRequired() {
				return false;
			}

			@Override
			public boolean isNextParallelPage(WebTask wikiWebTask,
					WebURL currentParallelWebUrl, WebURL filterParallelWebUrl) {
				return false;
			}

			@Override
			public boolean isParallelProcess() {
				return false;
			}

			@Override
			public void processListURLs(WebTask wikiWebTask) {
				if (wikiWebTask.getListWebUrl().size() > 0) {

					String pageName = "?page=";
					int pageNameLength = pageName.length();

					List<WebURL> listWebURL = wikiWebTask.getListWebUrl();
					String domain = listWebURL
							.get(0)
							.getURL()
							.substring(
									0,
									listWebURL.get(0).getURL()
											.indexOf(pageName)
											+ pageNameLength);

					List<Integer> listPageNumbers = new ArrayList<Integer>();

					for (WebURL webURL : listWebURL) {
						int value = Integer.parseInt(webURL.getURL().substring(
								webURL.getURL().indexOf(pageName)
										+ pageNameLength));
						listPageNumbers.add(value);
					}
					int maxPageNumber = Collections.max(listPageNumbers);

					wikiWebTask.setListWebUrl(new ArrayList<WebURL>());
					for (int i = 0; i < maxPageNumber + 1; i++) {
						WebURL webURL = new WebURL();
						webURL.setURL(domain + i);
						wikiWebTask.addWebURL(webURL);
					}
				}
			}
		});

		setMainWebTask(new WebTask(pagesWikiWebTask));
		getMainWebTask().addParametrToRules("/pl/publikacje/");
		getMainWebTask().addParametrToRules(0);
		getMainWebTask().setCurWebURL(startWebUrl);

		getMainWebTask().addIPageActions(new IPageActions() {

			@Override
			public void afterProcessPage(WebTask wikiWebTask, Page page,
					HtmlParseData htmlParseData) {
			}

			@Override
			public void beforeProcessPage(HtmlParseData htmlParseData) {
			}

			@Override
			public boolean filterUrl(WebURL webURL, WebTask webTask) {
				for (WebURL tmpWebURL : webTask.getListWebUrl()) {
					if (tmpWebURL.getURL().equalsIgnoreCase(webURL.getURL())) {
						return false;
					}
				}

				int countBack = 0;
				String startUrl = null;
				for (Object obj : webTask.getFilterRules().getListParameter()) {
					if (obj instanceof Integer) {
						countBack = (Integer) obj;
					} else if (obj instanceof String) {
						startUrl = (String) obj;
					}
				}

				if (webURL.getURL().contains(startUrl)) {
					if (Tools.countOccurencesOfSubString("/", webURL.getPath()
							.replace(startUrl, "")) == countBack) {
						return true;
					}
				}
				return false;
			}

			@Override
			public void finishProcessWebTasks(WebTask wikiWebTask) {
			}

			@Override
			public WebURL getFilterParallelWebUrl(FilterRules wikiFilterRules,
					List<WebURL> listWebUrl) {
				return null;
			}

			@Override
			public HttpRequestMethod getHttpRequestMethod() {
				return HttpRequestMethod.GET;
			}

			@Override
			public List<BasicNameValuePair> getParameterValuePairs(
					HashMap<String, String> mapParameters) {
				return null;
			}

			@Override
			public boolean isCookieRequired() {
				return false;
			}

			@Override
			public boolean isNextParallelPage(WebTask wikiWebTask,
					WebURL currentParallelWebUrl, WebURL filterParallelWebUrl) {
				return false;
			}

			@Override
			public boolean isParallelProcess() {
				return false;
			}

			@Override
			public void processListURLs(WebTask wikiWebTask) {
			}
		});
	}
}
